package com.platform.modules.mall.activityV2.restrict;

import com.platform.common.utils.Constant;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.Md5Utils;
import com.platform.modules.mall.activity.entity.bo.ActivityGoshoppingBo;
import com.platform.modules.mall.activity.entity.bo.ActivityResultStatus;
import com.platform.modules.mall.activityV2.entity.ParticipantBo;
import com.platform.modules.mall.entity.MallActivityEntity;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallWhitelistEntity;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import com.platform.modules.mall.service.MallOrderService;
import com.platform.modules.mall.service.MallWhitelistService;
import com.platform.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 白名单等级的过滤
 */
@Component("whitelistLevelRestrict")
public class WhitelistLevelRestrict implements RestrictInterfaceV2{

    @Autowired
    private MallGoodsToActivityService mallGoodsToActivityService;
    @Autowired
    protected MallWhitelistService mallWhitelistService;
    @Autowired
    protected SysConfigService sysConfigService;
    @Autowired
    private MallOrderService mallOrderService;
    /**
     * 获取抽奖对象并，减扣白名单数量
     * @param bo
     * @return  参与抽奖对象：剩余抽取的次数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ParticipantBo restrict(ActivityGoshoppingBo bo, MallActivityEntity mallActivityEntity) {

        ParticipantBo participantBo = getParticipantBo(bo, mallActivityEntity,true);

        return participantBo;
    }

    /**
     * 获取抽奖对象，不减扣白名单数量
     * @param bo
     * @param mallActivityEntity
     * @return
     */
    @Override
    public ParticipantBo restrictQuery(ActivityGoshoppingBo bo, MallActivityEntity mallActivityEntity) {

        ParticipantBo participantBo = getParticipantBo(bo, mallActivityEntity,false);

        return participantBo;
    }

    /**
     *    *  1.查看是否有未支付的订单
     *      *  2.查看该活动该用户所有的白名单信息 list
     *      *      2.1 验证用户手机是否有白名单是否满足数量
     *      *
     *      *  3.组装抽奖对象并返回
     *      *      3.1 查看能参与抽奖的所有商品
     *      *
     *      *  4.减扣白名单数量
     * @param bo
     * @param mallActivityEntity
     * @param deduction
     * @return
     */
    private ParticipantBo getParticipantBo(ActivityGoshoppingBo bo, MallActivityEntity mallActivityEntity,boolean deduction) {
        /*需要支付的活动，查看是否有未支付订单*/
        if (1== mallActivityEntity.getNeedToPay()){
            List<MallOrderEntity> list = mallOrderService.lambdaQuery()
                    .eq(MallOrderEntity::getUserId, bo.getUserId())
                    .eq(MallOrderEntity::getPayStatus, 1)
                    .eq(MallOrderEntity::getOrderStatus,0)
                    .eq(MallOrderEntity::getActivityId, bo.getActivityId()).list();
            if (list!=null&&list.size()>0){
                throw ActivityResultStatus.buildException(ActivityResultStatus.Status.ORDER_PAY_NO);
            }
        }

        /*2 查看白名单列表*/
        //是否md5加密
        String isMd5= sysConfigService.getValue("WHITELIST_MD5");
        List<MallWhitelistEntity> whitelist= mallWhitelistService.selectWhiteListByPhone(
                Constant.STR_ONE.equals(isMd5)? Md5Utils.encrypt(bo.getPhone()): bo.getPhone(),
                mallActivityEntity.getWhitelistType() );
        /*2.1   验证是否有白名单信息*/
        if (whitelist==null||whitelist.size()==0){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NOT_WHITE_LIST);
        }
        /*2.2   判断是否有百名数量大于领取数量
         * */
        MallWhitelistEntity mallWhitelistEntity=null;
        /*白名单数量*/
        int whiteListCount=0;
        for (MallWhitelistEntity s:whitelist){
            if (DateUtils.compare(s.getLapseTime(),new Date(),null)){
                if (s.getNumber()>= bo.getNumber()&&mallWhitelistEntity==null){
                    /**
                     * 拿到第一个符合条件的白名单
                     *  可以领取的数量就是这个白名单的数量，不对所有白名单数量做合计等级不同有影响
                     * */
                    mallWhitelistEntity=s;
                    whiteListCount=s.getNumber();
                }
            }
        }

        /*减扣白名单数量*/
        if (mallWhitelistEntity==null){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NOT_WHITE_LIST_NUMBER);
        }
        /*值查看信息时不减扣白名单*/
        if (deduction){
            mallWhitelistEntity.setNumber(mallWhitelistEntity.getNumber()- bo.getNumber());
            mallWhitelistService.update(mallWhitelistEntity);
        }

        /*3 组装抽奖对象*/
        ParticipantBo participantBo =new ParticipantBo();

        /*3.1 获取可抽奖的商品列表*/
        Map<String, Object> params =new HashMap<>();
        params.put("activityId", bo.getActivityId());
        params.put("isDelete",1);
        params.put("isOnSale",1);
        params.put("level",mallWhitelistEntity.getLevel());
        params.put("orderbySort"," order by T.SORT ");
        List<MallGoodsToActivityEntity> list = mallGoodsToActivityService.queryAll(params);

        participantBo.setNum(whiteListCount);
        participantBo.setList(list);
        participantBo.setGoodsId(bo.getGoodsId());
//        participantBo.setActivityType(mallActivityEntity.getActivityType());
        return participantBo;
    }
}
